import Vue from "vue";
import Vuex from "vuex";

Vue.use(Vuex);
import createVuexAlong from "vuex-along";
// import{checkloginByCode}from '@/api/administrator.js'

export default new Vuex.Store({
  state: {
    menu: [],
    token: null,
    isAddRoute: false,
  },
  getters: {
    getToken(state) {
      return state.token;
    },
    getParentMenu(state) {
      return state.menu.filter((item) => item.pid == 0);
    },
    getTopMenu(state) {
      return state.menu.filter((item) => item.pid == 0 && item.postion == "top");
    },
    getLeftMenu(state) {
      return state.menu.filter((item) => item.pid == 0 && item.postion == "left");
    },
    getPower(state) {
      return function (url) {
        if(!url) return (url)
        let id = state.menu.find((item) => item.url == url).id;
        return state.menu.filter((item) => item.pid == id)
      }
    },
  },
  mutations: {
    setMenu(state, menu) {
      state.menu = menu;
    },
    setToken(state, token) {
      state.token = token;
    },
    setIsAddRoute(state, isAddRoute) {
      state.isAddRoute = isAddRoute;
    },
    clearState(state) {
      state.menu = [];
      state.token = null;
      state.isAddRoute = false;
    },
  },
  actions: {
    async loginAsync(context, logininfo) {
      // 将menu和token保存到vuex中
      context.commit("setMenu", logininfo.menu);
      context.commit("setToken", logininfo.token);
    },
    logoutAsync(context) {
      try {
        context.commit("clearState");
      } catch (error) {
        Vue.prototype.$message.error(error.message);
      }
    },
  },
  modules: {},
  plugins: [
    createVuexAlong({
      local: {
        list: ["isAddRoute"],
        isFilter: true,
      },
    }),
  ],
});
